﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Controls;
using System.IO;
using Excel;
namespace project_assess.prj_view
{
    public partial class prj_change : Office2007Form
    {
        Mfrm mfrm;
        DataBase m_db;
        public prj_change(Mfrm frm )
        {
            InitializeComponent();
            mfrm = frm;
            m_db = frm.m_db;
            load_data();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            choose_prj frm = new choose_prj(mfrm);
            frm.ShowDialog(this);
            load_data();
        }

        void load_data()
        {
            if (mfrm.curprjid == null)
            {
                mfrm.curprjid = "";
            }
            string sql; System.Data.DataTable dt;
            sql = "select [项目编号],[项目名称],[项目负责人],[项目经理],[地址],[班组长],[监测类型],[采购厂商]" +
                " from [projects] where [项目编号]='" + mfrm.curprjid + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            gridinfo.DataSource = dt;

            sql = "select [合同额],[工作量系数],[综合系数],[采购系数],[测算日期],[材料埋设],[工作量],"
                +"Round([工作量]*"+mfrm.get_para("工作量项目额度") +",2) as [项目额度]"+
           " from [projects] where [项目编号]='" + mfrm.curprjid + "'";
            dt = m_db.GetDataSet(sql).Tables[0];
            gridpara.DataSource = dt; 

            //sql="select [分项内容],[规格方法],[数量],[材料单价],[埋设单价],"
        }

        private void gridinfo_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != 0 || mfrm.curprjid == "")
            {
                return;
            }
            DataGridViewX grid = sender as DataGridViewX;
            message_frm frm;
            frm = new message_frm("是否要更改?", "询问", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                grid.Rows[0].Cells[e.ColumnIndex].Value = oldValue;
                frm.Close(); return;
            }
            frm.Close();
            
            string txt = grid.Columns[e.ColumnIndex].Name;
            string str = grid.Rows[0].Cells[e.ColumnIndex].Value.ToString();
            if (grid.Columns[e.ColumnIndex].ValueType.Name == typeof(String).Name
                || grid.Columns[e.ColumnIndex].ValueType.Name == typeof(DateTime).Name)
            {
                str = "'" + str + "'";
            }
            string sql;
            if (txt != "项目经理")
            {
                sql = "update [projects] set [" + txt + "]=" + str + " where [项目编号]='" +
                    gridinfo.Rows[0].Cells[0].Value.ToString() + "'";
                m_db.ExecuteSQL(sql);
                mfrm.msg("已完成修改");
            }
            else
            {
                str = str.Replace("'", "");
                if (str == oldValue.ToString())
                {
                    //没发生变化
                    return;
                }
                manager_change change = new manager_change(mfrm, str, oldValue.ToString());
                change.ShowDialog(this);
                if (change.yesno == false)
                {
                    grid.Rows[0].Cells[e.ColumnIndex].Value = oldValue;
                }
                else
                {
                    sql = "update [projects] set [" + txt + "]=" + string.Format("'{0}'",str) + " where [项目编号]='" +
                                       gridinfo.Rows[0].Cells[0].Value.ToString() + "'";
                    m_db.ExecuteSQL(sql);

                    sql = "insert into [manager_change] values('" + gridinfo.Rows[0].Cells[0].Value.ToString()
                        + "','" + change.oldname + "'," + change.slider2.Value + ",'','')";
                    m_db.ExecuteSQL(sql);

                    mfrm.msg("已完成修改");
                }
                change.Close();
            }
        }

        object oldValue = null;
        private void gridinfo_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            message_frm frm;
            DataGridViewX grid = sender as DataGridViewX;
            string txt = grid.Columns[e.ColumnIndex].Name;
            if (txt == "项目编号")
            {
                frm = new message_frm("项目编号无法修改", "错误", icon_img.error);
                frm.ShowDialog(this);
                frm.Close();
                e.Cancel = true;
                return;
            }
            oldValue = grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        }

        //导出到excel
        private void btnexport_Click(object sender, EventArgs e)
        {
        }

        //删除一个项目的所有信息
        private void btnprjdelete_Click(object sender, EventArgs e)
        {
            message_frm frm;
            if (mfrm.curprjid == null || mfrm.curprjid == "")
            {
                frm = new message_frm("工程编号不能为空", "错误", icon_img.alert1);
                frm.ShowDialog(this);
                frm.Close();
                return;
            }
            frm = new message_frm("是否要删除该项目所有信息", "确认", icon_img.query);
            frm.ShowDialog(this);
            if (frm.yesorno == false)
            {
                frm.Close();
                return;
            }
            frm.Close();
            string sql = "delete from [projects] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [material] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [workload] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [projects_added] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [assess_work] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [assess_predict] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [assess_material] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            sql = "delete from [assess_manage] where [项目编号]='" + mfrm.curprjid + "'";
            m_db.ExecuteSQL(sql);
            string table = mfrm.curprjid + "_para";
            if (m_db.IsTableInDataBase(table))
            {
                m_db.DeleteTable(table);
            }
            mfrm.curprjid = "";
            load_data();
            mfrm.msg("删除完毕!");
        }
        
    }
}
